#!/bin/bash

rm -f "error.txt"
rm -f "output.txt"
rm -f "test_outputs.xml"
rm -f "test_state.xml"

OK=false
echo -n "Test 6 - CALL & RETURN ..... "

../../spblaze -t kcpsm3 -o test -x test.hex > output.txt 2> error.txt < commands.txt

# REGISTERS
RESULT=$(xml_grep --text_only '//regbank' test_state.xml)
RESULT=${RESULT:0:18}
EXPECTED=$(xml_grep --text_only '//regbank' test_state_expected.xml)
EXPECTED=${EXPECTED:0:18}

# STACK
RESULT_STACK=$(xml_grep --text_only '//stack' test_state.xml)      
RESULT_STACK=${RESULT_STACK:0:12}
EXPECTED_STACK=$(xml_grep --text_only '//stack' test_state_expected.xml)      
EXPECTED_STACK=${EXPECTED_STACK:0:12}
                                                      
# SP
RESULT_SP=$(xml_grep --text_only '//sp' test_state.xml)
EXPECTED_SP=$(xml_grep --text_only '//sp' test_state_expected.xml)

# PC
RESULT_PC=$(xml_grep --text_only '//pc' test_state.xml)
EXPECTED_PC=$(xml_grep --text_only '//pc' test_state_expected.xml)

# C
RESULT_C=$(xml_grep --text_only '//carry' test_state.xml)
EXPECTED_C=$(xml_grep --text_only '//carry' test_state_expected.xml)

# Z
RESULT_Z=$(xml_grep --text_only '//zero' test_state.xml)
EXPECTED_Z=$(xml_grep --text_only '//zero' test_state_expected.xml)

if [ $RESULT = $EXPECTED -a $RESULT_STACK = $EXPECTED_STACK -a $RESULT_PC = $EXPECTED_PC -a $RESULT_C = $EXPECTED_C -a $RESULT_Z = $EXPECTED_Z ]
then        
  OK=true
  echo "OK"
else
  echo "FAIL"
fi

if $OK
then
  exit 0
else
  exit 1
fi

 